package api

import (
	"github.com/gin-gonic/gin"
	"mogutouERP/models"
	"mogutouERP/utils/response"
	"net/http"
)

// CreateCustomer 创建一个客户
func CreateCustomer(c *gin.Context) {
	var data struct {
		Name         string `json:"name" binding:"required"`
		Tel          string `json:"tel"`
		Manager      string `json:"manager" binding:"required"`
		Address      string `json:"address"`
		WeiXin       string `json:"weiXin"`
		QQ           string `json:"qq"`
		EmailAddress string `json:"emailAddress"`
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}
	if models.HaveCustomerName(data.Name) {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: "客户已存在"})
		return
	}

	customer := models.Customer{
		Name:         data.Name,
		Tel:          data.Tel,
		Manager:      data.Manager,
		WeiXin:       data.WeiXin,
		QQ:           data.QQ,
		EmailAddress: data.EmailAddress,
		Address:      data.Address,
	}

	err = models.CreateCustomer(&customer)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, models.GetCustomer(customer.CustomerID))
}

// DeleteCustomer 删除客户信息
func DeleteCustomer(c *gin.Context) {
	customerID := c.Param("id")
	if !models.HaveCustomer(customerID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此客户信息"})
		return
	}

	err := models.DeleteCustomer(customerID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusNoContent, gin.H{})
}

// UpdateCustomer 更新客户信息
func UpdateCustomer(c *gin.Context) {
	customerID := c.Param("id")
	if !models.HaveCustomer(customerID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此客户信息"})
		return
	}

	var data struct {
		Name         string `json:"name"`
		Tel          string `json:"tel"`
		Manager      string `json:"manager"`
		EmailAddress string `json:"emailAddress"`
		WeiXin       string `json:"weiXin"`
		QQ           string `json:"qq"`
		Address      string `json:"address"`
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	err = models.UpdateCustomer(customerID, data.Name, data.Tel, data.Manager, data.Address, data.WeiXin, data.QQ, data.EmailAddress)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, models.GetCustomer(customerID))
}

// GetCustomer 获取客户信息
func GetCustomer(c *gin.Context) {
	customerID := c.Param("id")

	if !models.HaveCustomer(customerID) {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: "没有此客户信息"})
		return
	}

	customer := models.GetCustomer(customerID)

	c.JSON(http.StatusOK, gin.H{
		"name":         customer.Name,
		"tel":          customer.Tel,
		"manager":      customer.Manager,
		"weiXin":       customer.WeiXin,
		"qq":           customer.QQ,
		"emailAddress": customer.EmailAddress,
		"address":      customer.Address,
	})

}

// ListCustomers 列举所有客户信息
func ListCustomers(c *gin.Context) {
	customers, err := models.ListCustomers()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, customers)
}
